/*
 Copyright 2009 Christian Sprecher

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
 */
package widgets

import javax.swing.TransferHandler
import javax.swing.JTree
import javax.swing.tree.TreePath
import java.awt.datatransfer.Transferable
import java.awt.datatransfer.UnsupportedFlavorException
import javax.swing.tree.DefaultMutableTreeNode
import java.awt.datatransfer.DataFlavor
import javax.swing.JComponent
import java.awt.event.InputEvent
import javax.activation.DataHandler
import widgets.JcrDefaultMutableTreeNode
import helpers.JcrTreeHelper
import java.awt.datatransfer.Clipboard


public class JcrTransferHandler extends TransferHandler {
    def mimeTypes = ["jcr/node", "text/plain"]
    def session

    private int currentAction

    public void exportAsDrag(JComponent comp,InputEvent e,int action) {
        super.exportAsDrag(comp,e,action)
        this.currentAction=action
    }

    public void exportToClipboard(JComponent comp,Clipboard clip,int action) {
        super.exportToClipboard(comp, clip, action)
        this.currentAction = action
    }

    public boolean canImport(TransferHandler.TransferSupport support) {
        int i = 0
        def flavour1 = support.transferable.transferDataFlavors[0]
        return mimeTypes.any { flavour1.mimeType.startsWith(it)}
    }

    public boolean importData(TransferHandler.TransferSupport support) {
        if (!canImport(support)) return false

        def transferData
        def firstFlavour = support.transferable.transferDataFlavors[0]
        try {
            transferData = support.transferable.getTransferData(firstFlavour)
        } catch (Exception e) {
            return false
        }
        JcrTreeHelper helper = new JcrTreeHelper(support.component, session)
        def parentTreeNode
        try {
            parentTreeNode = support.dropLocation.path.lastPathComponent
        } catch (java.lang.IllegalStateException e) {
            parentTreeNode = support.component.lastSelectedPathComponent
        }

        if (firstFlavour.mimeType == "jcr/node") {
            if(currentAction==COPY) {
            helper.copyNode(parentTreeNode, transferData)
            }else {
              //assume cut action
              helper.moveNode(parentTreeNode,transferData)
            }
        } else {
            helper.addNode(parentTreeNode, transferData)
        }
        return true;
    }

    int getSourceActions(JComponent c) {
        return COPY_OR_MOVE;
    }


    protected Transferable createTransferable(JComponent c) {
//        def jcrNode = ['name': c.lastSelectedPathComponent.name, 'path': c.lastSelectedPathComponent.userObject]
//        def jcrNode = ['node': c.lastSelectedPathComponent.name, 'path': c.lastSelectedPathComponent.userObject]
        DataHandler dh = new DataHandler(c.lastSelectedPathComponent, "jcr/node")
        return dh
        //return super.createTransferable(c);    //To change body of overridden methods use File | Settings | File Templates.
    }

}